<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js循环结构之for..of</title>
  <style>
    ul {
      list-style: none;
    }
    li {
      display: flex;
      align-items: center;
      border: 1px solid #ccc;
      padding: 10px;
      cursor: pointer;
      width: 300px;
      height: 40px;
    }
  </style>
</head>
<body>
  <ul>
    <li>for..of可遍历数组或字符串</li>
    <li>对数组而言item为当前的数据项</li>
    <li>对字符串而言item为字符串中的字符</li>
  </ul>
  <script>
    // 遍历数组
    const books = [
      { title: 'JS学习', author: 'Miracle He' },
      { title: 'ES学习', author: 'Tom Wang' },
      { title: 'TS学习', author: 'Jack Li' }
    ]
    document.write('<table width="100%" border="1">')
    document.write('<thead><tr><th>标题</th><th>作者</th></tr></thead>')
    for (const item of books) {
      document.write(`
        <tr>
          <td>${item.title}</td>
          <td>${item.author}</td>
        </tr>`
      )
    }
    document.write('</table>')
    // 遍历字符串
    for (const item of 'Miracle') {
      console.log(item)
    }
    // 遍历DOM结构
    const lis = document.querySelectorAll('li')
    for (const item of lis) {
      item.addEventListener('click', function () {
        this.style.backgroundColor = 'red'
      })
    }
  </script>
</body>
</html>
